import 'package:flutter/material.dart';

// import 'package:travel/values/theme/color.dart';

///绘制圆角的[UnderlineTabIndicator]
///
/// Used with [TabBar.indicator] to draw a horizontal line below the
/// selected tab.
///
/// The selected tab underline is inset from the tab's boundary by [insets].
/// The [borderSide] defines the line's color and weight.
///
/// The [TabBar.indicatorSize] property can be used to define the indicator's
/// bounds in terms of its (centered) widget with [TabBarIndicatorSize.label],
/// or the entire tab with [TabBarIndicatorSize.tab].
class TabIndicator extends Decoration {
  /// Create an underline style selected tab indicator.
  ///
  /// The [borderSide] and [insets] arguments must not be null.
  const TabIndicator({
    this.borderSide = const BorderSide(width: 2, color: Colors.white),
    this.borderRadius = const BorderRadius.all(Radius.circular(2)),
    this.gradient,
    this.maxWidth,
    this.insets = EdgeInsets.zero,
  });

  /// 创建一个默认的渐变色的Indicator
  factory TabIndicator.gradient({
    EdgeInsets insets = const EdgeInsets.only(bottom: 12),
    BorderSide borderSide = const BorderSide(width: 6),
    BorderRadius borderRadius = const BorderRadius.only(
      topLeft: Radius.circular(3),
      bottomRight: Radius.circular(3),
    ),
    Gradient? gradient = const LinearGradient(colors: [Color(0xFFA6D3FF), Color(0xFFA9FAFA)]),
  }) {
    return TabIndicator(
      insets: insets,
      borderSide: borderSide,
      borderRadius: borderRadius,
      gradient: gradient,
    );
  }

  /// The color and weight of the horizontal line drawn below the selected tab.
  final BorderSide borderSide;

  final BorderRadius borderRadius;

  /// 最大宽度,为null时，默认绘制与文本等宽。
  final double? maxWidth;

  final Gradient? gradient;

  /// Locates the selected tab's underline relative to the tab's boundary.
  ///
  /// The [TabBar.indicatorSize] property can be used to define the tab
  /// indicator's bounds in terms of its (centered) tab widget with
  /// [TabBarIndicatorSize.label], or the entire tab with
  /// [TabBarIndicatorSize.tab].
  final EdgeInsetsGeometry insets;

  @override
  Decoration? lerpFrom(Decoration? a, double t) {
    if (a is TabIndicator) {
      return TabIndicator(
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
        insets: EdgeInsetsGeometry.lerp(a.insets, insets, t)!,
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
  Decoration? lerpTo(Decoration? b, double t) {
    if (b is TabIndicator) {
      return TabIndicator(
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
        insets: EdgeInsetsGeometry.lerp(insets, b.insets, t)!,
      );
    }
    return super.lerpTo(b, t);
  }

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _UnderlinePainter(this, onChanged, maxWidth: maxWidth);
  }

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
    return Rect.fromLTWH(
      indicator.left,
      indicator.bottom - borderSide.width,
      indicator.width,
      borderSide.width,
    );
  }

  @override
  Path getClipPath(Rect rect, TextDirection textDirection) {
    return Path()..addRect(_indicatorRectFor(rect, textDirection));
  }
}

class _UnderlinePainter extends BoxPainter {
  final TabIndicator decoration;
  final double? maxWidth;

  _UnderlinePainter(this.decoration, VoidCallback? onChanged, {this.maxWidth})
      : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration.size != null);
    Rect rect = offset & configuration.size!;
    if (maxWidth != null && maxWidth! < rect.width) {
      var width = maxWidth!;
      rect = Rect.fromLTRB(
        rect.left + rect.width / 2 - width / 2,
        rect.top,
        rect.right - rect.width / 2 + width / 2,
        rect.bottom,
      );
    }
    final TextDirection textDirection = configuration.textDirection!;
    final Rect indicator = decoration
        ._indicatorRectFor(rect, textDirection)
        .deflate(decoration.borderSide.width / 2.0);

    final Paint paint = decoration.borderSide.toPaint()
      ..strokeCap = StrokeCap.square
      ..shader = decoration.gradient?.createShader(indicator);

    ///绘制圆角矩形
    // var radius = Radius.circular(decoration.borderSide.width / 2);
    // var radius = Radius.circular(decoration.borderSide.width / 2);
    canvas.drawRRect(
      RRect.fromLTRBAndCorners(
        indicator.bottomLeft.dx,
        indicator.bottomLeft.dy - decoration.borderSide.width / 2,
        indicator.topRight.dx,
        indicator.bottomRight.dy,
        topLeft: decoration.borderRadius.topLeft,
        topRight: decoration.borderRadius.topRight,
        bottomLeft: decoration.borderRadius.bottomLeft,
        bottomRight: decoration.borderRadius.bottomRight,
      ),
      paint,
    );
  }
}
